package com.jic.member.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MemMemberCustomLabel;
import com.jic.member.entity.MememberLabel;
import com.jic.member.enums.EXCEPTION;
import com.jic.member.mapper.MemMemberCustomLabelMapper;
import com.jic.member.mapper.MememberLabelMapper;
import com.jic.member.request.MemMemberCustomLabelRequest;
import com.jic.member.response.MemMemberCustomLabelResponse;
import com.jic.member.service.MemMemberCustomLabelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class MemMemberCustomLabelServiceImpl implements MemMemberCustomLabelService {

    @Resource
    private MemMemberCustomLabelMapper memMemberCustomLabelMapper;
    @Resource
    private MememberLabelMapper mememberLabelMapper;
    @Resource
    private SysDeptApi sysDeptApi;



    @Override
    public RestResult insert(MemMemberCustomLabelRequest memMemberCustomLabelRequrst) {
        RestResult result = new RestResult();
        MemMemberCustomLabel memMemberCustomLabel = new MemMemberCustomLabel();
        memMemberCustomLabel.setDeleteFlag(0);
        memMemberCustomLabel.setDeptId(memMemberCustomLabelRequrst.getAdminOperate().getDeptId());
        memMemberCustomLabel.setDeptType(memMemberCustomLabelRequrst.getAdminOperate().getDeptType().getCode());
        memMemberCustomLabel.setCreator( memMemberCustomLabelRequrst.getAdminOperate().getOperator());
        memMemberCustomLabel.setCreatorName(memMemberCustomLabelRequrst.getAdminOperate().getOperateName());
        memMemberCustomLabel.setCreateTime(new Date());
        memMemberCustomLabel.setType(memMemberCustomLabelRequrst.getType());
        memMemberCustomLabel.setLabelName(memMemberCustomLabelRequrst.getLabelName());
        final long l = memMemberCustomLabelMapper.queryByDeptId(memMemberCustomLabel);
        if(l > 0){
            result.setCode(EXCEPTION.FAIL.getName());
            result.setMsg("同组织内标签名称不可重复");
        }else {
            if (null != memMemberCustomLabelRequrst.getId()){
                memMemberCustomLabel.setId(memMemberCustomLabelRequrst.getId());
                final long update = memMemberCustomLabelMapper.update(memMemberCustomLabel);
                HashMap<String,Object> map=new HashMap();
                map.put("type",1);
                map.put("levelName",memMemberCustomLabelRequrst.getLabelName());
                map.put("deptId",memMemberCustomLabelRequrst.getAdminOperate().getDeptId());
                map.put("deptType",memMemberCustomLabelRequrst.getAdminOperate().getDeptType().getCode());
                map.put("id",memMemberCustomLabelRequrst.getId());
                result.setCode(EXCEPTION.SUCCESS.getName());
            }else {
                final long insert = memMemberCustomLabelMapper.insert(memMemberCustomLabel);
                HashMap<String,Object> map=new HashMap();
                map.put("type",1);
                map.put("levelName",memMemberCustomLabelRequrst.getLabelName());
                map.put("deptId",memMemberCustomLabelRequrst.getAdminOperate().getDeptId());
                map.put("deptType",memMemberCustomLabelRequrst.getAdminOperate().getDeptType().getCode());
                map.put("id",memMemberCustomLabel.getId());
                if(insert > 0){
                    result.setCode(EXCEPTION.SUCCESS.getName());
                }
            }

        }
        return result;
    }

    @Override
    public long delete(Long id) {
        return memMemberCustomLabelMapper.delete(id);
    }

    @Override
    public long update(MemMemberCustomLabelRequest memMemberCustomLabelRequrst) {
        MemMemberCustomLabel memMemberCustomLabel = BeanUtil.copy(memMemberCustomLabelRequrst, MemMemberCustomLabel.class);
        memMemberCustomLabel.setUpdateTime(new Date());
        memMemberCustomLabel.setDeptId(memMemberCustomLabelRequrst.getAdminOperate().getDeptId());
        memMemberCustomLabel.setCreator( memMemberCustomLabelRequrst.getAdminOperate().getOperator());
        memMemberCustomLabel.setCreatorName(memMemberCustomLabelRequrst.getAdminOperate().getOperateName());
        return memMemberCustomLabelMapper.update(memMemberCustomLabel);
    }

    @Override
    public MemMemberCustomLabel queryByPrimaryKey(Long id) {
        return memMemberCustomLabelMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MemMemberCustomLabel> query(MemMemberCustomLabel memMemberCustomLabel) {
        AdminOperate adminOperate = memMemberCustomLabel.getAdminOperate();
        memMemberCustomLabel.setDeptId(adminOperate.getDeptId());
        memMemberCustomLabel.setDeptType(adminOperate.getDeptType().getCode());
        memMemberCustomLabel.setDeleteFlag(0);
        return memMemberCustomLabelMapper.query(memMemberCustomLabel);
    }

    @Override
    public RestResult<PageResult<MemMemberCustomLabelResponse>> queryPage(MemMemberCustomLabelRequest memMemberCustomLabelRequrst) {
        RestResult result = new RestResult();

        //计算下标
        int startIndex = (memMemberCustomLabelRequrst.getStart() - 1) * memMemberCustomLabelRequrst.getLimit();

        if (null == memMemberCustomLabelRequrst.getDeptId()){
            memMemberCustomLabelRequrst.setDeptId(memMemberCustomLabelRequrst.getAdminOperate().getDeptId());
        }

        List<MemMemberCustomLabel> list = memMemberCustomLabelMapper.queryPage(startIndex, memMemberCustomLabelRequrst.getLimit(), memMemberCustomLabelRequrst);
        long count = memMemberCustomLabelMapper.queryPageCount(memMemberCustomLabelRequrst);
        list.forEach(items -> {
            RestResult<String> name = sysDeptApi.queryDeptNameById(Long.valueOf(items.getDeptId()));
            items.setDeptName(name.getData());
        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        result.setCode("0000");
        result.setData(pageResult);
        return result;
    }

    @Override
    public RestResult deleteInfo(MemMemberCustomLabelRequest request) {
        RestResult result = new RestResult();
        //查询会员标签关联表
        MememberLabel memberLabel = new MememberLabel();
        memberLabel.setLabelId(request.getId());
        memberLabel.setDeleteFlag(0);
        List<MememberLabel> resultLabel = mememberLabelMapper.query(memberLabel);
        if(CollectionUtils.isNotEmpty(resultLabel)){
            result.setCode(EXCEPTION.FAIL.getName());
            result.setMsg("该标签已关联会员，请解除关联后在进行操作");
        }else {
            memMemberCustomLabelMapper.delete(request.getId());
            HashMap<String,Object> map=new HashMap();
            map.put("type",2);
            map.put("deptId",request.getAdminOperate().getDeptId());
            map.put("deptType",request.getAdminOperate().getDeptType().getCode());
            map.put("id",request.getId());
            result.setCode(EXCEPTION.SUCCESS.getName());
            result.setMsg(EXCEPTION.SUCCESS.getDesc());
        }
        return result;
    }
}
